package com.example.javatest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javatest.entity.domain.SupCreateclass;
import com.example.javatest.entity.domain.TbClass;
import com.example.javatest.entity.domain.TbUser;
import com.example.javatest.mapper.TbClassMapper;
import com.example.javatest.mapper.TbUserMapper;
import com.example.javatest.service.SupCreateclassService;
import com.example.javatest.mapper.SupCreateclassMapper;
import org.springframework.jdbc.object.UpdatableSqlQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

/**
* @author cac_qin
* @description 针对表【sup_createclass】的数据库操作Service实现
* @createDate 2023-12-14 21:26:17
*/
@Service
public class SupCreateclassServiceImpl extends ServiceImpl<SupCreateclassMapper, SupCreateclass>
    implements SupCreateclassService{

    @Resource
    SupCreateclassMapper supCreateclassMapper;
    @Resource
    TbClassMapper tbClassMapper;
    @Resource
    TbUserMapper tbUserMapper;

    /**
     * 提交 新建班级 申请
     * @param className
     * @param userName
     * @return
     */
    @Override
    public boolean supCreateClass(String className, String userName) {
        SupCreateclass supCreateclass = new SupCreateclass();
        supCreateclass.setSupClassname(className);
        supCreateclass.setSupSubname(userName);
        boolean saveResult = this.save(supCreateclass);
        return  saveResult;
    }

    @Override
    public boolean handleClassAply(String className, String isHandle,String Master,int supId) {

        //申请存不存在
        QueryWrapper<SupCreateclass> queryWrapper1 =new QueryWrapper<>();
        queryWrapper1.eq("sup_ClassName",className);
        int count1 = supCreateclassMapper.selectCount(queryWrapper1);
        if(count1<0){
            return false;
        }
        //教师是否存在
        QueryWrapper<TbUser> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_Name",Master);
        int count2 = tbUserMapper.selectCount(queryWrapper2);
        if(count2<0){
            return false;
        }

       //处理
        SupCreateclass supCreateclass = supCreateclassMapper.selectOne(queryWrapper1);
        if(isHandle.equals("NO")){
            supCreateclass.setSupHandel("被驳回");
            //修改班级申请表的处理状态
            supCreateclassMapper.updateById(supCreateclass);
        }

        if(isHandle.equals("YES")){
            //班级名称是否已经存在
            QueryWrapper<TbClass> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("Class_Name",className);
            int count3 = tbClassMapper.selectCount(queryWrapper3);
            if(count3>0){
                return false;
            }
            //新建班级
            TbClass tbClass = new TbClass();
            tbClass.setClassMaster(Master);
            tbClass.setClassName(className);
            tbClassMapper.insert(tbClass);

            //获取新建班级的classId
            queryWrapper3.eq("Class_Name",className);
            tbClass = tbClassMapper.selectOne(queryWrapper3);

            //修改用户用户所属班级
            TbUser tbUser = tbUserMapper.selectOne(queryWrapper2);
            tbUser.setClassId(tbClass.getClassId());
            tbUserMapper.updateById(tbUser);

            //修改班级申请表的处理状态
            System.out.println(supCreateclass);
            supCreateclass.setSupHandel("已通过");
            supCreateclassMapper.updateById(supCreateclass);
        }
        return true;
    }
}




